<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_ad_info extends ad_info {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    /*
     * 	取出一条信息
     */

    public function do_detail($ai_id = '') {
        $detail = array();
        $ai_id = isset($_REQUEST['ai_id']) ? $_REQUEST['ai_id'] : '';
        $detail = $this->one_info('ad_info', 'ai_id', $ai_id);
        //DEBUG 调试输出
        include template('ad/info/detail');
    }

    /*
     *  添加系统信息
     */

    public function do_add() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $request_data = isset($_REQUEST['ad_info']) && !empty($_REQUEST['ad_info']) ? $_REQUEST['ad_info'] : '';
        //$tmp = array("color" => '', "url" => '', "img" => '', "name" => '', "ai_sort" => '',ac_id=>1);
        //ai_bgcolor ai_name ad_link ai_path ai_name ai_sort
        //$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if(!$_G['user_id']){
        	$return['code'] = '1';
        	echo format_data($return, 'json', $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        	return false;
        }
        
        $so_id = $this->one_so_info('user_detail', 'user_id', $_G['user_id']);
        
        if ($request_data) {
            if ($_G['gp_api']) {
                $data=array();
                //DEBUG 目前只有一个广告默认1
                if(empty($request_data['ac_id'])){
                    $request_data['ac_id']=1;
                }
                $data['ai_bgcolor'] = $request_data['color'];
                $data['ad_link'] = $request_data['url'];
                $data['ai_path'] = $request_data['img'];
                $data['ai_name'] = $request_data['name'];
                $data['ai_sort'] = $request_data['ai_sort'];
                $data['ac_id'] = $request_data['ac_id'];
                $data['so_id'] = $so_id?$so_id:0;
                $data['create_dateline'] = TIMESTAMP;
                $insert_id = $this->add('ad_info', $data);
                if ($insert_id) {
                    $return['code'] = '0';
                    $return['data']['id'] = (string)$insert_id;
                } else {
                    $return['code'] = '1';
                }
            }
        } else {
            $return['code'] = '1';
        }
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }

    
    /*
     *  编辑广告
     */
    
    public function do_adedit(){
    	global $_G;    	
    	$request_data = isset($_REQUEST['ad_info']) && !empty($_REQUEST['ad_info']) ? $_REQUEST['ad_info'] : '';
    	if(!$_G['user_id']||!$request_data['id']){
    		$return['code'] = '1';
    		echo format_data($return, 'json', $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
    		return false;
    	}    
    	//网校id
    	if($_G['member']['role_id']==82){  //网校理员权限
    		$so_id = $this->one_so_info('user_detail', 'user_id', $_G['user_id']);
    	}else{
    		$so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : '0';
    	}   	   	   
    	if ($_G['gp_api']){  
    		$data['place_id'] = $request_data['id'];
    		$data['ai_bgcolor'] = $request_data['color'];
    		$data['ad_link'] = $request_data['url'];
    		$data['ai_path'] = $request_data['img'];
    		$data['ai_name'] = $request_data['name'];
    		$data['ai_sort'] = $request_data['ai_sort'];    		
    		$data['ac_id'] = 2;   		
    		if($so_id){
    			$data['so_id'] = $so_id;
    		}   		
    		$data['modify_dateline'] = TIMESTAMP;
    		$where = array('place_id' => $request_data['id'],'ac_id'=>$data['ac_id'],'so_id' => $so_id);
    		$effect_row = $this->edit_info('ad_info', $data, $where);
    		if ($effect_row) {
    			$return['code'] = '0';    			
    		} else {
    			$return['code'] = '1';
    		}
    	}    	 
    	echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }
    
    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $request_data = isset($_REQUEST['ad_info']) && !empty($_REQUEST['ad_info']) ? $_REQUEST['ad_info'] : '';
        if(!$_G['user_id']||!$request_data['id']){
        	$return['code'] = '1';
        	echo format_data($return, 'json', $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        	return false;
        }
        
        //网校id
        if($_G['member']['role_id']==82){  //网校理员权限
        	$so_id = $this->one_so_info('user_detail', 'user_id', $_G['user_id']);
        }else{
        	$so_id = isset($_REQUEST['so_id']) ? $_REQUEST['so_id'] : '0';
        }        
        //$so_id = $this->one_so_info('user_detail', 'user_id', $_G['user_id']);   
        //$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        
            if ($_G['gp_api']) {
            	$data['ai_id'] = $request_data['id'];
                $data['ai_bgcolor'] = $request_data['color'];
                $data['ad_link'] = $request_data['url'];
                $data['ai_path'] = $request_data['img'];
                $data['ai_name'] = $request_data['name'];
                $data['ai_sort'] = $request_data['ai_sort'];                             
                if($so_id){
                    $data['so_id'] = $so_id; 
                }
                //$data['ai_path'] = $request_data['img'];
                //$data['ad_link'] = $request_data['url'];
                $data['modify_dateline'] = TIMESTAMP;             
                $where = array('ai_id' => $request_data['id'],'ac_id'=>1);              
                $effect_row = $this->edit_info('ad_info', $data, $where);
                if ($effect_row) {
                    $return['code'] = '0';
                    //$return['data']['id'] = $request_data['id'];
                } else {
                    $return['code'] = '1';
                }
            }
       
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }

    /*
     *  删除系统信息
     */

    public function do_delete() {
        global $_G;
        //ini_set("display_errors","On");
        //error_reporting(E_ALL);
        $request_data = isset($_REQUEST['ad_info']) && !empty($_REQUEST['ad_info']) ? $_REQUEST['ad_info'] : '';
        //$id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
        if ($request_data['id']) {
            if ($_G['gp_api']) {
                $where = array('ai_id' => $request_data['id']);
                $effect_row = $this->delete("ad_info", $where, $limit = 1);
                if ($effect_row) {
                    $return['code'] = '0';
                } else {
                    $return['code'] = '1';
                }
            }
        } else {
            $return['code'] = '1';
        }
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do'],'pc','msg_admin','admin');
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $wheresql = "";
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        if ($keyword) {
            $wheresql = " AND ac_id LIKE '%" . $keyword . "%' ";
        }
        $wheresql .= " AND sm_id = '" . $_G['member']['sm_id'] . "' ";
        $orderby = " ORDER BY ai_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->index('ad_info', $page_condition);
        $multipage = multi($page_result['total_rows'], $perpage, $page, "index.php?mod=ad&action=info&do=index&keyword=" . $keyword);
        include template('ad/info/index');
    }

    /**
     * 接口 AJAX或外部请求检索数据
     *
     * @param $return_type string json/xml.
     *
     * @return print string json/xml.
     */
    public function do_api() {
        $return = '';
        $page = max(1, intval($_GET['page']));
        $perpage = $limit = 20;
        $start = (($page - 1) * $perpage);
        $keyword = isset($_REQUEST['keyword']) ? $_REQUEST['keyword'] : '';
        $return_type = isset($_REQUEST['return_type']) ? $_REQUEST['return_type'] : 'json';
        $wheresql = "";
        if ($keyword) {
            $wheresql = " WHERE title LIKE '%" . $keyword . "%'";
        }
        $orderby = " ORDER BY ai_id DESC ";
        //DEBUG 初始化请求请求获取一页列表数据的参数
        $page_condition = array(
            'page' => $page, //int 请求页面 页码
            'limit' => $limit, //int 每页请求个数
            'perpage' => $perpage, //int 每页显示个数
            'wheresql' => $wheresql, //string //条件SQL语句 
            'orderby' => $orderby, //string 排序规则
        );
        //DEBUG 列表数据返回结构
        /*
          $page_result = array(
          //int 返回结果总数
          'total_rows' => $total_rows,
          //array 一页数据数组
          'page_data' => $page_data
          );
         */
        $page_result = $this->api('ad_info', $page_condition);
        if ('json' == $return_type) {
            $return = json_encode($page_result);
        } elseif ('xml' == $return_type) {
            $return = array2xml($page_result);
        }
        //DEBUG 接口响应直接输出返回
        echo $return;
    }

}

?>